﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleTesrt.LINQTest
{
    public class LinqObject3
    {
        static void Maine33e(string[] ars)
        {
            //todo:查找字符串中包含的大写字母，字符串是由多个char类型组成
            //过滤字符换中所有的大写字母,判断小写用IsLetter,数字用IsDigit，标点用IsPunctuation,控制字符用IsControl,分隔符用 IsSeparator,符号类型用
            //IsSymbol,例如“=”,空白类型字符用IsWhiteSpace
            //查找字符串中包含数字，用char.IsDigit(char类型的)
            //查找字符串中标点符号，用char.IsPunctuation(char类型的)
            string string1 = "int I=10;string String1=(I%2==0?\"偶数\":\"奇数\")";
            var querystr = from a in string1
                           where char.IsLower(a)
                           select a;
            foreach (var item in querystr)
            {
                Console.Write(item + ",");
            }
            //todo:过滤文章中包含指定单词的句子
            //let子句在查询表达式中，存储子表达式的结果有时很有用，这样可以在随后的子句中使用。
            //可以使用let关键字完成这一工作，该关键字可以创建一个新的范围变量，并且用提供的表达式的结果初始化该变量。
            //一旦用值初始化了该范围的变量，他就不能用于存储其他值。如果该范围变量存储的是可查询的类型，则可以对齐进行查询
            Console.WriteLine("过滤文章指定的单词");
            string text = "While taking my boat down the inland waterway to Florida a few weeks ago, I decided to tie up at Georgetown, South Carolina, for the night and visit with an old friend. As we approached the Esso dock, I saw him through my binoculars standing there awaiting us. Tall and straight as an arrow he stood, facing a cold, penetrating wind—truly a picture of a sturdy man, even though his next birthday will make him eighty-two. Yes, the man was our elder statesman, Bernard Baruch.  He loaded us into his station wagon and we were off to his famous Hobcaw Barony for dinner. We sat and talked in the great living room where many notables and statesmen, including Roosevelt and Churchill, have sat and taken their cues. In his eighty-second year, still a human dynamo, Mr. Baruch talks not of the past but of present problems and the future, deploring our ignorance of history, economics, and psychology. His only reference to the past was to tell me, with a wonderful sparkle in his eye, that he was only able to get eight quail out of the ten shots the day before. What is the secret of this great man’s value to the world at eighty-one? The answer is his insatiable desire to keep being productive.  Two of the hardest things to accomplish in this world are to acquire wealth by honest effort and, having gained it, to learn how to use it properly. Recently I walked into the locker room of a rather well-known golf club after finishing a round. It was in the late afternoon and most of the members had left for their homes. But a half-dozen or so men past middle age were still seated at tables talking aimlessly and drinking more than was good for them. These same men can be found there day after day and, strangely enough, each one of these men had been a man of affairs and wealth, successful in business and respected in the community. If material  prosperity were the chief requisite for happiness, then each one should have been happy. Yet, it seemed to me, something very important was missing, else there would not have been the constant effort to escape the realities of life through Scotch and soda. They knew, each one of them, that their productivity had ceased. When a fruit tree ceases to bear its fruit, it is dying. And it is even so with man.  What is the answer to a long and happy existence in this world of ours? I think I found it long ago in a passage from the book, Genesis, which caught my eyes while I was thumbing through my Bible. The words were few, but they became indelibly impressed on my mind: “In the sweat of thy face shalt thou eat thy bread.”  To me, that has been a challenge from my earliest recollections. In fact, the battle of life, of existence, is a challenge to everyone. The immortal words of St. Paul, too, have been and always will be a great inspiration to me. At the end of the road I want to be able to feel that I have fought a good fight, I have finished the course, I have kept the faith.";
            string matches = "value"; //要查询的单词
            string[] sentences = text.Split(new char[] { '.', '?', '!', ',' }); //按句子转换成数组
            var query2 = from item in sentences
                         let words = item.Split(new char[] { '.', '?', '!', ';', ':', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)//将句子按单词转换成数组
                         where words.Contains(matches)
                         select item;//返回的是包含该单词的二次数组
            Console.WriteLine("含有" + matches + "的句子是：");
            foreach (var item in query2)
            {
                Console.WriteLine(item.ToString());
            }
            //todo:统计每个单词在一个字符串中出现的次数（使用Distinct()去掉重复的）
            //原理：将字符串转成数组A，在使用Distinct()方法获得数组A的去重版本数组B
            //     然后创建一个数组C存储数量，长度肯定是数组B的长度，这样数组B和数组C的长度一样，正好一个单词，对应一个数量，方便以后输出结果
            //     遍历整个数组B，获得临时单词，以这个临时值作为条件用linq在数组A中找这个单词
            //     然后找到后，在循环中用数组C保存找到的符合的同一个单词的出现的频率
            //     以达到数组B和数组C一一对应，方便以后输出的效果
            Console.WriteLine("过滤文章指定的单词");
            string text2 = "While taking my A boat down the inland waterway to Florida a few a weeks ago, I decided to tie up at Georgetown";
            //拆分成以每一个单词为一个元素的字符串数组
            string[] allWords = text2.Split(new char[] { '.', '?', '!', ';', ':', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            //去掉单词数组中重复的单词，用Distinct()方法
            string[] distinctWords = allWords.Distinct().ToArray();
            //创建一个存放词频统计信息的数组,以不重复的单词的数量为长度
            int[] counts = new int[distinctWords.Length];
            //遍历每一个单词,每次以这个单词在allWords数组中查找，因为allWords没有去掉重复的
            for (int i = 0; i < distinctWords.Length; i++)
            {
                string tempWord = distinctWords[i];//在不重复的单词数组中一个个获得
                var query3 = from item in allWords
                             where item.ToLower() == tempWord.ToLower()
                             select item;
                counts[i] = query3.Count();
            }
            //输出词频统计结果
            for (int i = 0; i < counts.Count(); i++)
            {
                Console.WriteLine(distinctWords[i] + "出现了" + counts[i] + "次");
            }
            string text3 = "角度让解放军解放军的考试开始看9988293，博客地址：谢谢你们";
            int cnt = 0;
            Regex rgx = new Regex("^[\u4E00-\u9FA5]{0,}$");
            for (int i = 0; i < text3.Length; i++)
            {
                cnt = rgx.IsMatch(text3[i].ToString()) ? ++cnt : cnt;
            }
            Console.WriteLine("”" + text3 + "“中文有" + cnt.ToString() + "个");
            Regex reg = new Regex(@"img/b");
            var regquery = from a in allWords
                           where reg.Match(a).Success == true
                           select a;
            //todo:将两个集合中的元素串联起来（集合1.concat(集合2)），concat是不去重复的
            int[] arr1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
            int[] arr2 = { 8, 6, 4, 3, 2, 1, 10 };
            var arr3 = arr1.Concat(arr2);
            Console.WriteLine("串联后：");
            foreach (var item in arr3)
            {
                Console.Write(item + ",");
            }
            //todo:也可以使用copyto
            //讲解CopyTo，数组1.CopyTo(数组2，在数组2中，数组1中的值插入的在数组2中索引位置)
            //arr1.CopyTo(arr4, 0);  在arr4的索引0的位置上开始复制arr1中的数据，arr1中一共9个值，也就是占据了arr4的0-8的位置
            //arr2.CopyTo(arr4,9);   然后在arr4的索引9的位置上开始复制arr2中的数据
            //如果不是9是4的话，那么arr1在arr4的4开始（包括4）的位置到索引8的位置的值都被arr2替换了
            int[] arr4 = new int[arr2.Length + arr1.Length];
            arr1.CopyTo(arr4, 0);
            arr2.CopyTo(arr4, 9);
            Console.WriteLine("arr4的结果：");
            foreach (var item in arr4)
            {
                Console.Write(item + ",");
            }
            /******************************************************************/
            //todo:使用LINQ操作文件
            //取两个目录中的同名文件
            /* string dir1 = @"D:\LinqStudy\linq1";
             string dir2 = @"D:\LinqStudy\linq2";
             List<FileInfo> files1 = new List<FileInfo>();
             List<FileInfo> files2 = new List<FileInfo>();
             foreach (string file in Directory.GetFiles(dir1))
             {
                 files1.Add(new FileInfo(file));
             }
             foreach (string file in Directory.GetFiles(dir2))
             {
                 files2.Add(new FileInfo(file));
             }
             var query24 = from file1 in files1
                          join file2 in files2
                          on file1.Name equals file2.Name
                          orderby file1.Name
                          select file1;
             foreach (var item in query24)
             {
                 Console.WriteLine(string.Format("{0}\t{1}", item.Name, item.Length));
             }
             * //todo:查找文件名中还有.NET的后缀名为doc类型的文件
             *  var query=from file in files
                           where file.Extension==.doc&&file.Name.IndexOf(.NET)>-1
                           orderby file.Name
                           select file;
             * //todo:想在某一个文件夹下查找1个G以上的文件  
             * var query=from file in files
                           where file.Length>1000000000
                           orderby file.Length
                           select file;
             * //todo:查找只读文件
             *  var query=from file in files
                           where (file.Attributes & FileAttributes.ReadOnly)!=0
                           select file;
             * //todo: 查找包含指定内容的文件
             * Console.WriteLine();
            string findStr = "LinqStudy";
            Console.WriteLine("包含" + findStr + "文字的txt文件如下：");
            var query3 = from file in files1
                         where file.Extension == ".txt"
                         let content = File.ReadAllText(file.FullName, System.Text.Encoding.Default)
                         where content.Contains(findStr)
                         orderby file.Name
                         select file;
            foreach (var item in query3)
            {
                Console.WriteLine(string.Format("{0}\t{1}", item.Name, item.Length));
            }
             */
        }
    }
}
